package soccerBet.project.funds;


import java.util.Date;
import java.util.TimerTask;
import java.util.concurrent.Semaphore;

import soccerBet.project.betfair.exchange.BFExchangeServiceStub.GetAccountFundsResp;
import soccerBet.project.betfair.global.BFGlobalServiceStub.GetEventsResp;
import soccerBet.project.betfair.handler.ExchangeAPI;
import soccerBet.project.betfair.handler.GlobalAPI;
import soccerBet.project.betfair.handler.ExchangeAPI.Exchange;
import soccerBet.project.main.FlowManager;
import soccerBet.project.main.ServiceManager;
import soccerBet.project.utils.Display;

public class Funds extends TimerTask {

	public static double avalableFunds = 0.0;
	public long tempoFinal = 0;
	public Semaphore LoginSem;
	
	public Funds(long tempoFinal){
		this.tempoFinal=(tempoFinal+(110*60*1000));
	}
	
	
	public void run() {
		
		ServiceManager sM = new ServiceManager();
		
		//Login

		sM.efectuaLogin();
		//EXCEEDED_THROTTLE
		Display.println("Login efectuado...");
		
		while(new Date().getTime() < this.tempoFinal){
		
				try {

					GetAccountFundsResp funds = ExchangeAPI.getAccountFunds(Exchange.UK, ServiceManager.apiContext);
		
					if(funds.getAvailBalance()<=15)
						avalableFunds = 0;
					else
						avalableFunds = (int)Math.round(((double)funds.getAvailBalance() * 0.1) * 100)/(double)100;
					
					Display.println("Funds Avalable: "+avalableFunds+"\n");
					
					Thread.sleep(20000);
					
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
		}
		
		//Logout
		Display.println("A efectuar logout...\n");
		sM.efectuaLogout();
		Display.println("Logout efectuado...\n");
		
		this.cancel();
		
	}
	
}
